package com.hnxxxy.sysmgr.service;

import com.hnxxxy.sysmgr.mapper.ISysCacheMgrMapper;
import com.hnxxxy.sysmgr.mapper.ISysConfigMapper;
import com.hnxxxy.sysmgr.mapper.ISysMapper;
import com.hnxxxy.sysmgr.pojo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @CLASSDESCRIPER:
 * @AUTHOR:suhui
 * @PHONE:13319575368
 * @EMIAL:13319575368@163.COM
 * @DATE: 2020/3/31
 * @PARAMDESCRIPER:
 * @OUTPARAMDESCRIPER:
 */
@Service
public class SysService {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ISysMapper sysMapper;

    @Autowired
    private ISysConfigMapper configMapper;

    @Autowired
    private ISysCacheMgrMapper iSysCacheMgrMapper;

    public Staff toLogin(String userName, String passWord){
            return sysMapper.toLogin(userName,passWord);
    }

    public Project getProjectInfo(){
        return sysMapper.getProjectInfo();
    }


    public StaffCollection getStaffCollectionInfo(Staff staff) {
        //具有优化空间
        StaffCollection staffCollection=new StaffCollection();
        staffCollection.setStaff(staff);
        List<Role> roleList=sysMapper.getStaffRole(staff.getId());
        staffCollection.setRoleList(roleList);
        List<Token> tokenList=sysMapper.getStaffToken(staff.getId());
        System.out.println(tokenList);
        List<Token> pTokenList=new ArrayList<>();
        List<Token> hTokenList=new ArrayList<>();
        List<Token> dTokenList=new ArrayList<>();
        for (Token token:tokenList) {
            //数据令牌
            if(token.getType()==0){
                dTokenList.add(token);
            }else if(token.getType()==1 && token.getMenutype().equals("p")){
                pTokenList.add(token);
            }else if(token.getType()==1 && token.getMenutype().equals("h")) {
                hTokenList.add(token);
            }
        }
        List<MenuTree> menuTrees=this.getMenuTree(pTokenList);
        staffCollection.setMenuTrees(menuTrees);
        staffCollection.setDToken(dTokenList);
        staffCollection.setHToken(hTokenList);
        return staffCollection;

    }

    public List<MenuTree> getMenuTree(List<Token> pTokenList){
        List<MenuTree> list=new ArrayList<>();
        if(pTokenList!=null && pTokenList.size()>0){
            //取第一层菜单
            for (Token token :pTokenList) {
                if(token.getParentid()==null){
                    MenuTree tree=new MenuTree();
                    tree.setMenuid(token.getMenuid().toString());
                    tree.setUrl(token.getUrl());
                    tree.setIcon(token.getIcon());
                    tree.setMenuname(token.getMenuname());
                    list.add(tree);
                }
            }
            for(MenuTree tree:list){
                this.getMenuTree1(tree,pTokenList);
            }
        }
        return list;
    }

    public void getMenuTree1(MenuTree tree, List<Token> pTokenList){
            List<MenuTree> treeList=new ArrayList<>();
            for(Token token:pTokenList){
                if(token.getParentid()!=null){
                    if(tree.getMenuid().equals(token.getParentid().toString())){
                        MenuTree tree1=new MenuTree();
                        tree1.setMenuid(token.getMenuid().toString());
                        tree1.setUrl(token.getUrl());
                        tree1.setIcon(token.getIcon());
                        tree1.setMenuname(token.getMenuname());
                        treeList.add(tree1);
                        this.getMenuTree1(tree1,pTokenList);
                    }
                }
            }
            tree.setChildren(treeList);
    }


    public Staff findByName(String username) {
       return  sysMapper.findByName(username);
    }

    public Set<String> findRolesByStaffId(Integer id) {
        return  sysMapper.findRolesByStaffId(id);
    }

    public Set<String> findPremsByStaffId(Integer id) {
        return  sysMapper.findPremsByStaffId(id);
    }

    public List getSelectList(String groupid) {
        return sysMapper.getSelectList(groupid);
    }

    public Config getSysConfigInfo(){
        return configMapper.getSysConfigInfo();
    }

    public List<CacheTableObj> getSysCacheInfo(){
        return iSysCacheMgrMapper.getSysCacheConif();
    }
}
